package com.wxg.snake;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.Random;

public class GamePanel extends JPanel implements KeyListener, ActionListener {
    int lenth;//蛇的长度
    int[] snakeX = new int[600];
    int[] snakey = new int[500];
    String fx;
    boolean isStart=false;//游戏是否开始
    Timer timer = new Timer(100,this);

    //定义一个食物
    int foodx,foody;
    Random random = new Random();
    //死亡判断
    boolean isFail=false;
    //积分系统
    int score;

    //构造器


    public GamePanel() {
        init();
        //获取键盘的监听事件
        this.setFocusable(true);//键盘焦点聚集在游戏上面
        this.addKeyListener(this);
        timer.start();
    }

    //初始化
    public void init(){
       lenth = 3;
       snakeX[0]=100;snakey[0]=100;//头部坐标
       snakeX[1]=75;snakey[1]=100;
       snakeX[2]=50;snakey[2]=100;
       fx = "R";
       foodx = 25 + 25* random.nextInt(34);
       foody = 75 + 25* random.nextInt(24);
       score=0;
    }


    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);//清屏
        this.setBackground(Color.white);
        //绘制头部广告栏
        Data.header.paintIcon(this,g,25,11);
        //绘制游戏区域
        g.fillRect(25,75,850,600);

        //画上蛇
        if(fx.equals("R")){
            Data.right.paintIcon(this,g,snakeX[0],snakey[0]);
        }else if (fx.equals("L")){
            Data.left.paintIcon(this,g,snakeX[0],snakey[0]);
        }else if (fx.equals("U")){
            Data.up.paintIcon(this,g,snakeX[0],snakey[0]);
        }else if (fx.equals("D")){
            Data.down.paintIcon(this,g,snakeX[0],snakey[0]);
        }


        for (int i = 1; i <lenth ; i++) {
            Data.body.paintIcon(this,g,snakeX[i],snakey[i]);
        }
        //画积分
        g.setColor(Color.cyan);
        g.setFont(new Font("微软雅黑",Font.BOLD,18));
        g.drawString("长度："+lenth,750,35);
        g.drawString("分数："+score,750,50);


        //画食物
        Data.food.paintIcon(this,g,foodx,foody);



        //游戏提示：是否开始
        if(isStart==false){
            g.setColor(Color.BLUE);//设置画笔的颜色
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("按下空格开始",300,300);
        }
        //失败提醒
        if(isFail){
            g.setColor(Color.red);//设置画笔的颜色
            g.setFont(new Font("微软雅黑",Font.BOLD,40));
            g.drawString("游戏失败，按下空格重新开始",200,300);
        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        //接收键盘输入
        int keyCode = e.getKeyCode();
        if(keyCode==KeyEvent.VK_SPACE){
            if(isFail){
                isFail=false;
                init();
            }else {
                isStart = !isStart;
            }

            repaint();//刷新界面
        }
        if(keyCode == KeyEvent.VK_RIGHT){
            fx="R";
        }else if(keyCode == KeyEvent.VK_LEFT){
            fx="L";
        }else if(keyCode == KeyEvent.VK_UP){
            fx="U";
        }else if(keyCode == KeyEvent.VK_DOWN){
            fx="D";
        }


    }

    @Override
    public void keyReleased(KeyEvent e) {

    }
    //定时器，监视时间，帧,执行定时操作
    @Override
    public void actionPerformed(ActionEvent e) {
        //如果游戏处于开始状态
        if(isStart && isFail==false){
            //向右移动
            for(int i = lenth-1;i>0;i--){
                snakeX[i]=snakeX[i-1];
                snakey[i]=snakey[i-1];
            }
            if(fx.equals("R")){
                snakeX[0] = snakeX[0] + 25;
                if(snakeX[0]>850){
                    snakeX[0]=25;
                }
            }else if (fx.equals("L")){
                snakeX[0] = snakeX[0] - 25;
                if(snakeX[0]<25){
                    snakeX[0]=850;
                }
            }else if (fx.equals("U")){
                snakey[0] = snakey[0] - 25;
                if(snakey[0]<75){
                    snakey[0]=650;
                }
            }else if (fx.equals("D")){
                snakey[0] = snakey[0] + 25;
                if(snakey[0]>650){
                    snakey[0]=75;
                }
            }
            //头和食物重合
            if(snakeX[0]==foodx && snakey[0]==foody){
                lenth++;
                score+=10;
                //重新生成食物
                foodx = 25 + 25* random.nextInt(34);
                foody = 75 + 25* random.nextInt(24);
            }
            //结束判断
            for (int i = 1; i <lenth ; i++) {
                {
                    if(snakeX[0]==snakeX[i] && snakey[0]==snakey[i]){
                        isFail=true;
                    }
                }

            }


            repaint();
        }
        timer.start();//让时间动起来
    }



}
